//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.example.mybatisplus.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HashUtil {
    private static final Logger logger = LoggerFactory.getLogger(HashUtil.class);

    public HashUtil() {
    }

    public static String hash(String source) {
        try {
            return new String(((String)Objects.requireNonNull(toHex(md5(source)))).getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
        } catch (Exception var2) {
            logger.error("not supported charset...", var2);
            return source;
        }
    }

    public static String hash(File file) {
        if (null == file) {
            throw new IllegalArgumentException("file is null");
        } else if (!file.exists()) {
            throw new IllegalArgumentException("file is not found");
        } else if (file.isDirectory()) {
            throw new IllegalArgumentException("file is a directory");
        } else {
            FileInputStream fileInputStream = null;

            Object var3;
            try {
                MessageDigest MD5 = MessageDigest.getInstance("MD5");
                fileInputStream = new FileInputStream(file);
                byte[] buffer = new byte[8192];

                int length;
                while((length = fileInputStream.read(buffer)) != -1) {
                    MD5.update(buffer, 0, length);
                }

                String var5 = bytesToHex(MD5.digest());
                return var5;
            } catch (Exception var15) {
                var15.printStackTrace();
                var3 = null;
            } finally {
                try {
                    if (fileInputStream != null) {
                        fileInputStream.close();
                    }
                } catch (IOException var14) {
                    var14.printStackTrace();
                }

            }

            return (String)var3;
        }
    }

    private static byte[] md5(String source) {
        try {
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(source.getBytes(StandardCharsets.UTF_8));
            return algorithm.digest();
        } catch (Exception var3) {
            logger.error("MD5 Error...", var3);
            return null;
        }
    }

    private static String toHex(byte[] hash) {
        if (hash == null) {
            return null;
        } else {
            StringBuilder builder = new StringBuilder(hash.length * 2);

            for(int i = 0; i < hash.length; ++i) {
                if ((hash[i] & 255) < 16) {
                    builder.append("0");
                }

                builder.append(Long.toString((long)(hash[i] & 255), 16));
            }

            return builder.toString();
        }
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder builder = new StringBuilder();
        byte[] var2 = bytes;
        int var3 = bytes.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            byte aByte = var2[var4];
            String hex = Integer.toHexString(aByte & 255);
            if (hex.length() < 2) {
                builder.append(0);
            }

            builder.append(hex);
        }

        return builder.toString();
    }
}
